package com.gift.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.gift.common.BaseServiceImpl;
import com.gift.common.Constant;
import com.gift.mapper.UserMapper;
import com.gift.domain.User;

import com.gift.service.UserService;
import com.gift.util.RUtils;
import com.gift.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by levy on 2019/4/9.
 */

@Service
@Transactional
public class UserServiceImpl extends BaseServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAll();
    }

    @Override
    public int insertList(List<User> list) {
        return userMapper.insertList(list);
    }

    @Override
    public R getRandomCodeImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        R r = RUtils.success();
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(105, 35, 4, 10);
        captcha.write(response.getOutputStream());
        String imageCode = captcha.getCode();
        setSessionAttr(Constant.IMG_CODE_SESSION, imageCode);
        return r;
    }
}
